<template>
    <el-row class="lives-news">
        <el-row>
            <el-col class="title">
                <h3>客户管理</h3>
            </el-col>
        </el-row>
        <el-row class="search-row">
            <el-form class="inline-form" :inline="true" @submit.native.prevent="getUser">
                <el-form-item>
                    <el-button type="primary" @click="showAddDialog"><i class="el-icon-plus el-icon--left"></i>添加客户</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="exportsEXL" :loading="downloading"><i class="el-icon-share el-icon--left"></i>导出</el-button>
                </el-form-item>
                <el-form-item label="搜索客户">
                    <el-input class="search-input" placeholder="请输入账号" icon="close" v-model="usernameLike" :on-icon-click="emptySearch">
                    </el-input>
                    <el-input class="search-input" placeholder="请输入客户昵称" icon="close" v-model="nameLike" :on-icon-click="emptySearch">
                    </el-input>
                </el-form-item><el-form-item><el-button type="primary" @click="getUser">搜索</el-button></el-form-item>
            </el-form>
        </el-row>
        <el-row>
            <el-col>
                <el-table :data="customList">
                    <el-table-column prop="userId" label="客户ID" width="120px" align="center"></el-table-column>
                    <el-table-column prop="username" label="账号" width="130px" align="center"></el-table-column>
                    <el-table-column label="昵称" width="140px" align="center">
                        <template slot-scope="scope">{{scope.row.name?scope.row.name:'-'}}</template>
                    </el-table-column>
                    <el-table-column prop="level" label="等级" width="80px" align="center"></el-table-column>
                    <el-table-column label="注册时间" align="center">
                        <template slot-scope="scope">{{moment(scope.row.registryTime).format('YYYY-MM-DD HH:mm:ss')}}</template>
                    </el-table-column>
                    <el-table-column label="登录时间" align="center">
                        <template slot-scope="scope">{{moment(scope.row.lastLoginTime).format('YYYY-MM-DD HH:mm:ss')}}</template>
                    </el-table-column>
                    <el-table-column label="观看时长" align="center">
                        <template slot-scope="scope">{{scope.row.watchTime}}分钟</template>
                    </el-table-column>
                    <el-table-column label="今日观看时长" align="center">
                        <template slot-scope="scope">{{scope.row.todayWatchTime}}分钟</template>
                    </el-table-column>
                    <el-table-column label="是否为员工号" align="center">
                        <template slot-scope="scope">{{scope.row.salesman?'是':'否'}}</template>
                    </el-table-column>
                    <el-table-column label="邀请码" align="center" prop="code"></el-table-column>
                    <el-table-column label="操作" width="140px" align="center">
                        <template slot-scope="scope">
                            <el-button type="primary" size="small" @click="updateCustom(scope.$index, scope.row)">修改</el-button>
                            <el-button type="danger" size="small" @click="deleteCustom(scope.$index, scope.row)">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </el-col>
        </el-row>
        <el-dialog :title="(isAdd ? '新增':'修改') + '客户'" :visible.sync="addDialog" size="tiny" @close="cancelAddCategory" class="dialog">
                <el-form :model="userInfo" label-width="120px">
                    <el-form-item label="客户手机">
                        <el-input v-model="userInfo.username" auto-complete="on" placeholder="请输入客户手机号"></el-input>
                    </el-form-item>
                    <el-form-item label="邀请码">
                        <el-input v-model="userInfo.code" auto-complete="on" placeholder="请输入邀请码"></el-input>
                    </el-form-item>
                    <el-form-item label="昵称" v-if="!isAdd">
                        <el-input v-model="userInfo.name" auto-complete="on" placeholder="请输入昵称"></el-input>
                    </el-form-item>
                    <el-form-item label="等级">
                        <el-radio-group v-model="userInfo.level">
                            <el-radio label="普通">普通</el-radio>
                            <el-radio label="白银">白银</el-radio>
                            <el-radio label="黄金">黄金</el-radio>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item label="内部账号">
                        <el-checkbox v-model="userInfo.salesman"></el-checkbox>
                    </el-form-item>
                </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="cancelAddCategory">取 消</el-button>
                <el-button type="primary" @click="addCategory" :loading="addLoading">保 存</el-button>
            </div>
        </el-dialog>
        <!-- 分页 -->
        <el-row>
            <el-col class="pagination">
                <el-pagination @current-change="currentChange" :current-page="pageId" :page-size="pageSize" layout="total, prev, pager, next" :total="counts">
                </el-pagination>
            </el-col>
        </el-row>
    </el-row>
</template>
<script>
import {
    getUserList,
    addUser,
    updateUser,
    deleteUser
} from '@/api/api'
import {exportUser} from '@/api/excel'
export default {
    data: function() {
        return {
            pageId: 1,
            pageSize: 10,
            counts: 0,
            customList: [],
            addDialog: false,
            userInfo: {
                userId: null,
            	name: '',
                secretkey: '',
                code: '',
                username: '',
                salesman: false,
                level: '普通'
            },
            addLoading: false,
            isAdd: true,
            downloading: false,
            nameLike: '',
            usernameLike: ''
        }
    },
    created: function() {
        this.pageId = parseInt(this.$route.query.page) || 1;
        this.getUser();
    },
    destroyed: function() {
        clearInterval(this.interVal)
        this.interVal = null;
    },
    methods: {
        //获取视频分类列表
        getUser: function() {
            var params = {
                pageSize: this.pageSize,
                pageId: this.pageId,
                nameLike: this.nameLike,
                usernameLike: this.usernameLike
            }
            getUserList({params: params}).then(data => {
                console.log(data)
                var str = '?';
                for (var key in params) {
                    if (params[key]) {
                        str += key + '=' + params[key] + '&'
                    }
                }
                this.$router.push(str);
                this.counts = data.count;
                this.customList = data.list;
            })
        },
        //搜索
        searchArticleByName: function() {
            this.getUser();
        },
        //分页
        currentChange: function(val) {
            this.$router.push('?page=' + val)
            this.pageId = val;
            this.getUser()
        },
        showAddDialog: function(){
            this.addDialog = true;
            this.isAdd = true;
        },
        cancelAddCategory: function(){
            this.addDialog = false;
            this.userInfo = {
                userId: null,
                name: '',
                secretkey: '',
                code: '',
                username: '',
                salesman: false,
                level: '普通'
            }
        },
        addCategory: function(){
            if(this.isAdd){
                this.userInfo.secretkey = this.userInfo.code;
                addUser(this.userInfo).then(res => {
                    this.$message({
                        type: 'success',
                        message: '操作成功'
                    });
                    this.getUser();
                    this.cancelAddCategory()
                })
            }else{
                updateUser(this.userInfo).then(res => {
                    this.$message({
                        type: 'success',
                        message: '操作成功'
                    });
                    this.getUser();
                    this.cancelAddCategory()
                })
            }
        },
        updateCustom: function(index, row){
           this.addDialog = true;
           this.isAdd = false;
           this.userInfo = {
                userId: row.userId,
                name: row.name,
                code: row.code,
                secretkey: row.code,
                username: row.username,
                salesman: row.salesman,
                level: row.level
           }
        },
        deleteCustom: function(index, row){
            this.$confirm('确定要删除该条客户信息?', '删除客户', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                deleteUser(row.userId).then(() => {
                    this.getUser();
                    this.$message({
                        type: 'success',
                        message: '操作成功!'
                    });
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '取消操作'
                });
            });
        },
        chunkBlob: function(data){
            var blob = new Blob([data], {type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8'})
            var link = document.createElement('a');
            link.href = window.URL.createObjectURL(blob);
            link.download = '注册用户';
            document.body.appendChild(link);
            link.click();
            document.body.removeChild(link);
            window.URL.revokeObjectURL(link.href);
            this.downloading = false;
        },
        exportsEXL: function(){
            if(this.downloading){
                this.$message({
                    type: 'error',
                    message: '正在导出中!'
                });
                return
            }
            this.downloading = !this.downloading;
            exportUser({params: {start: this.startTime, end: this.endTime}}).then(res => {
                this.chunkBlob(res.data)
            }).catch(e=>{
                this.$message({
                    type: 'error',
                    message: '导出出错，请稍后再试！'
                });
                this.downloading = false;
                console.log(e)
            })
        },
        emptySearch: function(){
            this.nameLike = ''
            this.usernameLike = ''
            this.getUser()
        }
    }
}
</script>
<style scoped lang="scss">
.lives-news {
    .title {
        font-size: 12px;
        border-bottom: 1px solid #eaeaea;
        margin-bottom: 5px;
        h3 {
            display: inline-block;
            padding: 5px 0;
            margin: 0;
            font-weight: normal;
            color: #23b7e5;
            border-bottom: 1px solid #23b7e5;
        }
    }
}
.search-input{
	width: 200px;
}
</style>